#!/usr/bin/env python

import re
import constants


class serviceCommandParser:

   def __init__(self,blend):
      self.blend = blend;
      
   def parse(self,message):
      commands = message.split("\n");
      self.out = "";
      for i in range(0,len(commands)):
         
         if commands[i].startswith("::op=cmd"):
            data = commands[i][9:].split("|");
            cmd = Command();
            for i in range(0,len(data)):
               #if i+1 == len(data):
                  #data[i] = data[i][:-1];

               (name,value) = data[i].split("=");
               name = str(name);
               value = str(value);
               if value.lower() == 'true':
                  value = True;
               elif value.lower() == 'false':
                  value = False;                 
               setattr(cmd,name,value);
            if cmd.isAutomatic() == True:
               cmd.runCommand(self.blend);
            try:
               self.out += cmd.generateOutput(self.blend) + "\n";
            except AttributeError:
               pass;
         else:
            self.out += commands[i] + "\n";

      return self.out;


class Command:

   type = "";
   id = "";


   def __init__(self):
      pass;

   def isAutomatic(self):
      try:
         if self.auto:
            return True;
         else:
            return False;
      except AttributeError:
         return False;

   # Sanitise message
   def message(self,attr):
      return attr.replace("%3a",":");

   def generateOutput(self,blend):
      if self.type == 'sendsms':
         # Unsupported feature
         return '<a href="blendit://sms">%s</a>' % self.selmsg;
      elif self.type == 'reply':
         m = self.selmsg.replace(":","");
         m = self.message(m);
         return '<a href="blendit://sendmessage:%s">%s</a>' % (self.replymsg,m);
      elif self.type == 'platreq':
         # Unsupported
         return '<a href="blendit://platreq">%s</a>' % self.selmsg;
      elif self.type == 'selc':
         hdng,msg,ct,filt,nm = '','','','','';
         if hasattr(self,'hdng'):
            hdng = self.hdng;
         if hasattr(self,'msg'):
            msg = self.msg;
         if hasattr(self,'ct'):
            ct = self.ct;
         if hasattr(self,'filt'):
            filt = self.filt;
         if hasattr(self,'nm'):
            nm = self.nm;

         return '<a href="blendit://selc:%s:%s:%s:%s:%s">%s</a> ' % (hdng,msg,ct,filt,nm,self.selmsg);
      elif self.type == 'img':
         # Do lotsa stuff;
         pass;

   def runCommand(self,blend):
      n = self.blend.gui.chatWindow.notebook.get_current_page();
      conv = self.blend.gui.chatWindow.convs.getByPageNumber(n);

      if type == 'clear':
         # Clear the conv
         conv.view.get_buffer().buf.set_text("");
      elif type == 'reply':
         buddy = conv.buddy;
         try:
            try:
               yn = self.noshow;
            except AttributeError:
               yn = True;

            self.mxitmessage.sendMessage(buddy.jid,self.message(self.replymsg),yn);
         except AttributeError:
            pass;
      